package com.tancire.sources.controller;

import com.tancire.common.annotation.Log;
import com.tancire.common.core.controller.BaseController;
import com.tancire.common.core.domain.AjaxResult;
import com.tancire.common.core.page.TableDataInfo;
import com.tancire.common.enums.BusinessType;
import com.tancire.common.utils.poi.ExcelUtil;
import com.tancire.file.bean.SaveFileBean;
import com.tancire.sources.domain.SourceCode;
import com.tancire.sources.domain.SurResourceDetail;
import com.tancire.sources.service.ISurResourceDetailService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 资源明细Controller
 *
 * @author chenchao
 * @date 2024-07-05
 */
@RestController
@RequestMapping("/sources/surResourceDetail")
public class SurResourceDetailController extends BaseController {

    @Resource
    private ISurResourceDetailService surResourceDetailService;

    /**
     * 查询资源明细列表
     */
    @PreAuthorize("@ss.hasPermi('sources:surResourceDetail:list')")
    @GetMapping("/list")
    public TableDataInfo list(SurResourceDetail surResourceDetail) {
        startPage();
        List<SurResourceDetail> list = surResourceDetailService.selectSurResourceDetailList(surResourceDetail);
        return getDataTable(list);
    }

    /**
     * 导出资源明细列表
     */
    @PreAuthorize("@ss.hasPermi('sources:surResourceDetail:export')")
    @Log(title = "资源明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SurResourceDetail surResourceDetail) {
        List<SurResourceDetail> list = surResourceDetailService.selectSurResourceDetailList(surResourceDetail);
        ExcelUtil<SurResourceDetail> util = new ExcelUtil<SurResourceDetail>(SurResourceDetail.class);
        util.exportExcel(response, list, "资源明细数据");
    }

    /**
     * 获取资源明细详细信息
     */
    @PreAuthorize("@ss.hasPermi('sources:surResourceDetail:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(surResourceDetailService.selectSurResourceDetailById(id));
    }

    /**
     * 新增资源明细
     */
    @PreAuthorize("@ss.hasPermi('sources:surResourceDetail:add')")
    @Log(title = "资源明细", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SurResourceDetail surResourceDetail) {
        return toAjax(surResourceDetailService.insertSurResourceDetail(surResourceDetail));
    }

    /**
     * 修改资源明细
     */
    @PreAuthorize("@ss.hasPermi('sources:surResourceDetail:edit')")
    @Log(title = "资源明细", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SurResourceDetail surResourceDetail) {
        return toAjax(surResourceDetailService.updateSurResourceDetail(surResourceDetail));
    }

    /**
     * 删除资源明细
     */
    @PreAuthorize("@ss.hasPermi('sources:surResourceDetail:remove')")
    @Log(title = "资源明细", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(surResourceDetailService.deleteSurResourceDetailByIds(ids));
    }

    /**
     * 获取观看视频授权码
     */
    @GetMapping("/view/code/{id}")
    public AjaxResult getViewCode(@PathVariable("id") Long id) {
        SourceCode code = new SourceCode();
        try {
            code = surResourceDetailService.getViewCode(id, false);
        } catch (Exception e) {
            code.setAuthStatus(3);
            code.setMsg("获取授权码错误");
            return AjaxResult.success(code);
        }
        return AjaxResult.success(code);
    }

    /**
     * 上传
     *
     * @param file
     * @return
     */
    @PostMapping("/upload")
    @ResponseBody
    public AjaxResult upload(@RequestParam("file") MultipartFile file, @RequestParam("resourceId") Long resourceId) {
        try {
            SaveFileBean saveFileBean = surResourceDetailService.uploadFile(file, resourceId);
            return AjaxResult.success(saveFileBean);
        } catch (Exception e) {
            return AjaxResult.error("上传文件异常:" + e.getMessage());
        }
    }
}
